 
  

 






<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do?Id=82 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:28:07 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
<head>
 <title>
  Java Practices -> Clipboard copy and paste
 </title>
 <link rel="stylesheet" type="text/css" href="../stylesheet8.css" media="all">
 
 <link rel="shortcut icon" href='../images/favicon.ico' type="image/vnd.microsoft.icon">
 <meta name="description" content="Concise presentations of java programming practices, tasks, and conventions, amply illustrated with syntax highlighted code examples.">
 
 <meta name='keywords' content='clipboard,copy,java,java programming,java practices,java idiom,java style,java design patterns,java coding conventions,'>
 
 
</head>
 
<body>


<div class='menu-bar'>
 
  <a href='../home/HomeAction.html' title='Table of Contents'>Home</a> |
  <a href='../vote/VoteSummaryAction-2.html' title='View Poll Results'>Poll</a> |
   
  <A href='../feedback/FeedbackAction451f-2.html?Operation=Show' title='Send Your Feedback'>Wiki</a> |
  <b><a href='../source/SourceAction-2.html' title='Grab Source Code'>Source Code</a></b><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |

  <a href='http://www.web4j.com/Java_Web_Application_Framework_Overview.jsp?From=1' title='Free Download - Java Web Application Framework'><b>WEB4J</b></a> |
  
  <a href='http://www.date4j.net/' title='Replacement for java.util.Date'><b>DATE4J</b></a> |

   <a href='../references/ReferencesAction-2.html' title='References'>Links</a>
   
  <form action='http://www.javapractices.com/search/SearchAction.do' method='get' class='search-form'>
   <input type='text' name='SearchTerms' value="" size=12 maxlength=50 class='search'>
   <input type='submit' value="Search">
  </form>
 
</div>

<P>



  

 






<p class="display-messages">

 

 

</p>


<div class="main-layout">
 
   

 




<div class='page-title'>Clipboard copy and paste</div>

<div class='main-body'>
 
<br>Here is a demonstration of how to move text to and from the <a href="http://java.sun.com/javase/6/docs/api/java/awt/datatransfer/Clipboard.html">Clipboard</a>.
<p>In Java, there are actually two kinds of&nbsp;<tt>Clipboard</tt> - system
and local. The following example uses the system&nbsp;<tt>Clipboard</tt>.
It corresponds to the usual idea of sharing data between otherwise independent
applications running on the same computer.
<p>Local&nbsp;<tt>Clipboard</tt>s are visible only within a single Java
application. They are created by simply passing a name to the <a href="http://java.sun.com/javase/6/docs/api/java/awt/datatransfer/Clipboard.html#Clipboard(java.lang.String)">Clipboard
constructor</a>.
<br>
<PRE>

<span class='keyword'>import</span> java.awt.datatransfer.Clipboard;
<span class='keyword'>import</span> java.awt.datatransfer.ClipboardOwner;
<span class='keyword'>import</span> java.awt.datatransfer.Transferable;
<span class='keyword'>import</span> java.awt.datatransfer.StringSelection;
<span class='keyword'>import</span> java.awt.datatransfer.DataFlavor;
<span class='keyword'>import</span> java.awt.datatransfer.UnsupportedFlavorException;
<span class='keyword'>import</span> java.awt.Toolkit;
<span class='keyword'>import</span> java.io.*;

<span class='keyword'>public</span> <span class='keyword'>final</span> <span class='keyword'>class</span> TextTransfer <span class='keyword'>implements</span> ClipboardOwner {

  <span class='keyword'>public</span> <span class='keyword'>static</span> <span class='keyword'>void</span> main (String...  aArguments ){
    TextTransfer textTransfer = <span class='keyword'>new</span> TextTransfer();

    <span class='comment'>//display what is currently on the clipboard
</span>    System.out.println(<span class='literal'>"Clipboard contains:"</span> + textTransfer.getClipboardContents() );

    <span class='comment'>//change the contents and then re-display
</span>    textTransfer.setClipboardContents(<span class='literal'>"blah, blah, blah"</span>);
    System.out.println(<span class='literal'>"Clipboard contains:"</span> + textTransfer.getClipboardContents() );
  }

   <span class='comment'>/**
   * Empty implementation of the ClipboardOwner interface.
   */</span>
   <span class='keyword'>public</span> <span class='keyword'>void</span> lostOwnership( Clipboard aClipboard, Transferable aContents) {
     <span class='comment'>//do nothing
</span>   }

  <span class='comment'>/**
  * Place a String on the clipboard, and make this class the
  * owner of the Clipboard's contents.
  */</span>
  <span class='keyword'>public</span> <span class='keyword'>void</span> setClipboardContents( String aString ){
    StringSelection stringSelection = <span class='keyword'>new</span> StringSelection( aString );
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    clipboard.setContents( stringSelection, <span class='keyword'>this</span> );
  }

  <span class='comment'>/**
  * Get the String residing on the clipboard.
  *
  * @return any text found on the Clipboard; if none found, return an
  * empty String.
  */</span>
  <span class='keyword'>public</span> String getClipboardContents() {
    String result = <span class='literal'>""</span>;
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    <span class='comment'>//odd: the Object param of getContents is not currently used
</span>    Transferable contents = clipboard.getContents(<span class='keyword'>null</span>);
    <span class='keyword'>boolean</span> hasTransferableText =
      (contents != <span class='keyword'>null</span>) &amp;&amp;
      contents.isDataFlavorSupported(DataFlavor.stringFlavor)
    ;
    <span class='keyword'>if</span> ( hasTransferableText ) {
      <span class='keyword'>try</span> {
        result = (String)contents.getTransferData(DataFlavor.stringFlavor);
      }
      <span class='keyword'>catch</span> (UnsupportedFlavorException ex){
        <span class='comment'>//highly unlikely since we are using a standard DataFlavor
</span>        System.out.println(ex);
        ex.printStackTrace();
      }
      <span class='keyword'>catch</span> (IOException ex) {
        System.out.println(ex);
        ex.printStackTrace();
      }
    }
    <span class='keyword'>return</span> result;
  }
} 
</PRE>
<br>
<br>An example run of this class :
<p><tt>>java -cp . TextTransfer</tt>
<br><tt>Clipboard contains:Java is in Indonesia.</tt>
<p><tt>Clipboard contains:blah, blah, blah</tt>
<p><tt>></tt>
<br>
<br>

</div>





<div class='topic-section'>Would you use this technique?</div>
<div class='main-body'>
  
  <form action="http://www.javapractices.com/vote/AddVoteAction.do" method='post'>
    Yes<input type='radio' name='Choice' value='Y' >
    &nbsp;&nbsp;No<input type='radio' name='Choice' value='N'>
    &nbsp;&nbsp;Undecided<input type='radio' name='Choice' value="?" >
    &nbsp;&nbsp;<input type=submit value="Vote" >
    <input type='hidden' name='Operation' value='Apply'>
    <input type='hidden' name='TopicId' value='82'>
  </form>
</div>

<div style='height:10.0em;'></div>

 
 
</div>

  

 





<div align='center' class='legalese'>  
&copy; 2011 Hirondelle Systems |
<a href='../source/SourceAction-2.html'><b>Source Code</b></a><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |
<a href="mailto:webmaster@javapractices.com">Contact</a> |
<a href="http://creativecommons.org/licenses/by-nc-sa/1.0/">License</a> |
<a href='../apps/cjp.rss'>RSS</a>
<!-- ukey="2AC36CD2" -->
<!-- ckey="16DF3D87" -->
<br>

 Individual code snippets can be used under this <a href='../LICENSE.txt'>BSD license</a> - Last updated on June 6, 2010.<br>
 Over 150,000 unique IPs last month - <span title='Java Practices 2.6.5, Mon May 16 00:00:00 EDT 2011'>Built with</span> <a href='http://www.web4j.com/'>WEB4J</a>.<br>
 - In Memoriam : Bill Dirani -
</div>

<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2633428-1";
urchinTracker();
</script>



</body>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do?Id=82 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:28:07 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
</html>
